package com.dykj.fireFighting.web.base.rate;

import com.dykj.fireFighting.api.base.dev.service.IDevHisService;
import com.dykj.fireFighting.api.base.dev.vo.RateVo;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: zly
 * @Description : 复核率
 * @Date: 2021-08-13
 */
@Slf4j
@Api(value = "复核率", tags = {"复核率"})
@RestController
@RequestMapping("devhis/rate")
public class DevHisRateController {

    @DubboReference
    private IDevHisService devHisService;
    @DubboReference
    private IEnterpriseService iEnterpriseService;

    @AutoLog(value = "复核率")
    @ApiOperation(value = "复核率", notes = "复核率")
    @RequestMapping("getReviewRate")
    public CommonResult getReviewRate(String beginDate,String endDate){
        LoginUserVo logUser = ShiroUtil.getUser();
        HashMap<String, Object> params = new HashMap<>(16);
        params.put("beginDate",beginDate);
        params.put("endDate",endDate);
        //社会单位:0，维保单位:1，监控中心:2
        if(0 == logUser.getEnterpriseType()){
            params.put("unitId",logUser.getEnterpriseId());
        }
        if(2 ==logUser.getEnterpriseType()){
            List<Enterprise> enterprises = iEnterpriseService.queryUnitSelect(logUser.getEnterpriseId().toString());
            if(enterprises.size()>0){
                params.put("enterprises",enterprises);
            }
        }
        //已复核总数
        double total=devHisService.getAlreadyReviewCount(params);
        if(0 ==total){
            return CommonResult.success(0);
        }else {
            //有效复核数
            double effect = devHisService.getEffectCount(params);
            double res=effect / total;
            BigDecimal bigDecimal = new BigDecimal(res);
            double result = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            double num =  result * 100;
            return CommonResult.success(num);
        }
    }

    /**
     * 折线图 本年度有效率对比
     */
    @AutoLog(value = "复核率")
    @ApiOperation(value = "复核率", notes = "复核率")
    @RequestMapping("/getMonthRate")
    public CommonResult getMonthRate(){
        LoginUserVo logUser = ShiroUtil.getUser();
        HashMap<String, Object> params = new HashMap<>(16);
        //社会单位:0，维保单位:1，监控中心:2
        if(0 == logUser.getEnterpriseType()){
            params.put("unitId",logUser.getEnterpriseId());
        }
        if(2 ==logUser.getEnterpriseType()){
            List<Enterprise> enterprises = iEnterpriseService.queryUnitSelect(logUser.getEnterpriseId().toString());
            if(enterprises.size()>0){
                params.put("enterprises",enterprises);
            }
        }
         List<RateVo> rateVos =devHisService.getMonthRate(params);
        return CommonResult.success(rateVos,"查询成功");
    }


}
